package com.qingfeng.coupon.service;

import com.qingfeng.base.exception.LsException;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.coupon.bean.CouponDistributeGoods;
import com.qingfeng.coupon.bean.CouponTemplate;
import com.qingfeng.coupon.bean.CouponUseGoods;
import com.qingfeng.coupon.constant.CouponCategory;
import com.qingfeng.coupon.constant.CouponDistributeType;
import com.qingfeng.coupon.constant.CouponUseType;
import com.qingfeng.coupon.dao.CouponDistributeGoodsMapper;
import com.qingfeng.coupon.dao.CouponTemplateMapper;
import com.qingfeng.coupon.dao.CouponUseGoodsMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/5/27 3:32 下午
 */
@Service
public class CouponTemplateService {

    @Autowired
    private CouponTemplateMapper couponTemplateMapper;

    @Autowired
    private CouponDistributeGoodsMapper distributeGoodsMapper;

    @Autowired
    private CouponUseGoodsMapper useGoodsMapper;

    /**
     * 保存template
     * 保存distributegoodslist
     * 保存 usegoodslist
     * @param couponTemplate
     */
    @Transactional
    public void save(CouponTemplate couponTemplate){
        couponTemplateMapper.insert(couponTemplate);
        saveDistributeAndUseGoods(couponTemplate);
    }


    @Transactional
    public void update(CouponTemplate couponTemplate){
        couponTemplate.setAvailable(null);
        couponTemplateMapper.updateById(couponTemplate);
        // 删除 usedGoodsList 和 distributeGoodsList
        distributeGoodsMapper.deleteByTemplateId(couponTemplate.getId());
        useGoodsMapper.deleteByTemplateId(couponTemplate.getId());
        // 添加 usedGooddsList和distributeGoodsList
        saveDistributeAndUseGoods(couponTemplate);
    }

    private void saveDistributeAndUseGoods(CouponTemplate couponTemplate){
        //购物赠券 如果是指定商品分发则保存list
        if (couponTemplate.getCategory().equals(CouponCategory.GOODS_COUPON)
                && couponTemplate.getDistributeType()
                .equals(CouponDistributeType.BUY_GOODS_DISTRIBUTE))
        {
            distributeGoodsMapper.insertList(
                    couponTemplate.getDistributeGoods(),
                    couponTemplate.getId()
            );
        }

        // 如果不是全场使用则保存list
        if (!couponTemplate.getUseType()
                .equals(CouponUseType.AMOUNT_USE))
        {
            if (CollectionUtils.isEmpty(couponTemplate.getUseGoods())){
                throw new LsException(CodeEnum.ERROR.getCode(),"请选择商品/分类");
            }
            useGoodsMapper.insertList(
                    couponTemplate.getUseGoods(),
                    couponTemplate.getId()
            );
        }
    }

    public CouponTemplate getDetail(long id){
        CouponTemplate couponTemplate = couponTemplateMapper.selectById(id);
        if (couponTemplate==null){
            throw new LsException(CodeEnum.ERROR.getCode(),"模版不存在");
        }
        List<CouponDistributeGoods> distributeGoods = distributeGoodsMapper.selectByTemplateId(id);
        List<CouponUseGoods> useGoods = useGoodsMapper.selectByTemplateId(id);
        couponTemplate.setDistributeGoods(distributeGoods);
        couponTemplate.setUseGoods(useGoods);
        return couponTemplate;
    }

}
